package com.strawcom.charmring.view;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.strawcom.charmring.R;
import com.strawcom.charmring.util.ViewUtil;

/**
 * 
 * @ClassName: SelectDialog
 * @Description:
 * @author liyongqiang
 * @date 2015-3-5 下午11:41:43
 * 
 */
public class SelectDialog {

	private Dialog mDialog;
	private ListView listView;
	private List<Map<String, String>> data;
	private LayoutInflater inflater;
	private Context context;

	public SelectDialog (Context context,
			List<Map<String, String>> data,int titleId,final OnItemClickListener itemClickListener) {
		this.data=data;
		this.context=context;
		inflater=LayoutInflater.from(context);
		mDialog = new Dialog(context, R.style.CustomProgressDialog);
		View content = LayoutInflater.from(context).inflate(
				R.layout.dialog_select, null);
		mDialog.setContentView(content);
		listView=(ListView) content.findViewById(R.id.list_view);
		TextView tvTitle=(TextView) content.findViewById(R.id.tx_title);
		tvTitle.setText(titleId);
		ViewUtil.changeFontsStyle(tvTitle, context);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				itemClickListener.onItemClick(arg0, arg1, arg2, arg3);
				mDialog.dismiss();
			}
		});
	}
	
	public void show() {
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.setCancelable(true);
		mDialog.show();
	}

	BaseAdapter adapter=new BaseAdapter() {
		
		@Override
		public View getView(int poistion, View cantentView, ViewGroup arg2) {
			cantentView=inflater.inflate(R.layout.view_select_item, null);
			TextView tv=(TextView) cantentView.findViewById(R.id.tv_text);
			Map<String, String> map=data.get(poistion);
			Set<String> keySet=map.keySet();
			Iterator<String> it= keySet.iterator();
			if(it.hasNext()){
				ViewUtil.changeFontsStyle(tv,context);
				String key=it.next();
				tv.setText(map.get(key));
				cantentView.setTag(key);
			}
			return cantentView;
		}
		
		@Override
		public long getItemId(int id) {
			return id;
		}
		
		@Override
		public Object getItem(int poistion) {
			return data.get(poistion);
		}
		
		@Override
		public int getCount() {
			return data.size();
		}
	};
}
